package ln.fxspringarchetype.vo;

import javafx.beans.property.*;
import ln.fxspringarchetype.model.Student;
import ln.fxspringarchetype.util.DateUtil;

import java.time.LocalDate;

/**
 * 学生视图模型
 */
public class StudentVO extends BaseFxViewModel<Student> {
    private final IntegerProperty id = new SimpleIntegerProperty();
    private final StringProperty name = new SimpleStringProperty();
    private final StringProperty gender = new SimpleStringProperty();
    private final ObjectProperty<LocalDate> birthday = new SimpleObjectProperty<>();
    private final ObjectProperty<byte[]> photo = new SimpleObjectProperty<>();

    public int getId() {
        return id.get();
    }

    public void setId(int id) {
        this.id.set(id);
    }

    public IntegerProperty idProperty() {
        return id;
    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public StringProperty nameProperty() {
        return name;
    }

    public String getGender() {
        return gender.get();
    }

    public void setGender(String gender) {
        this.gender.set(gender);
    }

    public StringProperty genderProperty() {
        return gender;
    }

    public LocalDate getBirthday() {
        return birthday.get();
    }

    public void setBirthday(LocalDate birthday) {
        this.birthday.set(birthday);
    }

    public ObjectProperty<LocalDate> birthdayProperty() {
        return birthday;
    }

    public byte[] getPhoto() {
        return photo.get();
    }

    public void setPhoto(byte[] photo) {
        this.photo.set(photo);
    }

    public ObjectProperty<byte[]> photoProperty() {
        return photo;
    }

    /**
     * 写入数据到实体类
     *
     * @return
     */
    @Override
    public Student toModel() {
        Student student = new Student();
        student.setId(getId());
        student.setName(getName());
        student.setGender(Student.Gender.of(getGender()));
        student.setPhoto(getPhoto());
        student.setBirthday(DateUtil.localDateToDate(getBirthday()));
        return student;
    }

    /**
     * 从实体类读取数据
     *
     * @param student
     */
    @Override
    public void fromModel(Student student) {
        this.setId(student.getId());
        this.setName(student.getName());
        this.setGender(student.getGender().getValue());
        this.setBirthday(DateUtil.dateToLocalDate(student.getBirthday()));
        this.setPhoto(student.getPhoto());
    }
}
